package com.tedu.ivos.application.service.impl;

import com.tedu.ivos.application.mapper.AppMapper;
import com.tedu.ivos.application.pojo.dto.AppQuery;
import com.tedu.ivos.application.pojo.dto.AppSaveFormParam;
import com.tedu.ivos.application.pojo.entity.App;
import com.tedu.ivos.application.pojo.vo.AppVO;
import com.tedu.ivos.application.service.IAppService;
import com.tedu.ivos.autit.mapper.AuditMapper;
import com.tedu.ivos.autit.pojo.vo.AuditVO;
import com.tedu.ivos.autit.service.impl.AuditService;
import com.tedu.ivos.base.enums.AppStatusEnum;
import com.tedu.ivos.user.mapper.UserMapper;
import com.tedu.ivos.user.pojo.vo.UserVO;
import com.tedu.ivos.vehicle.mapper.VehicleMapper;
import com.tedu.ivos.vehicle.pojo.entity.Vehicle;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.StringJoiner;

@Slf4j
@Transactional //spring的注解 事务性注解保持事务原子性
@Service
public class AppService implements IAppService {
    @Autowired
    AppMapper appMapper;
    @Autowired
    AuditService auditService;
    @Autowired
    UserMapper userMapper;
    @Autowired
    AuditMapper auditMapper;
    @Autowired
    VehicleMapper vehicleMapper;

    @Override
    public void saveApp(AppSaveFormParam appSaveFormParam) {
        log.debug("新增申请单业务，参数：{}", appSaveFormParam);
        App app = new App();
        BeanUtils.copyProperties(appSaveFormParam,app);
        //设置当前新增的申请单的初始状态为"已发起"
        app.setStatus(AppStatusEnum.PENDING.getCode());
        app.setCreateTime(new Date());
        /* 遇到了问题：新增申请单时，对应审批单数据中没有当前申请单的id
        * 原因：传给审批的申请单对象app的id属性没有值
        * 解决方案：给此方法对应的SQL 加上 useGeneratedKeys="true" keyProperty="id" 组合使用
        * 作用：JDBC会为当前的app对象自动生成自增主键并且把生成的主键值赋值给当前app对象的id属性
        */
        appMapper.insert(app);
        //还需要为此申请单生成对应的审批单数据 是与申请单同时生成
        auditService.insertAudit(app);

    }

    @Override
    public List<AppVO> select(AppQuery appQuery) {
        log.debug("查询申请单业务，参数：{}", appQuery);
        List<AppVO> list = appMapper.select(appQuery);
        //遍历每一个查询出来的申请单对象，依次为每个申请单补充审批人数据
        for(int i=0;i<list.size();i++){
            AppVO appVO = list.get(i);
            //处理当前遍历到的申请单，给它补充审批人数据
            assignAuditUserList(appVO);
        }
        return list;
    }

    private void assignAuditUserList(AppVO appVO) {
        //1.准备空集合，用来保存申请单对应的审批人姓名与id数据
        List<String> auditUsernameList = new ArrayList<>();
        List<Long> auditUserIdList = new ArrayList<>();
        List<AuditVO> auditVOList = auditService.selectAuditByAppId(appVO.getId());
        for(int i=0;i<auditVOList.size();i++){
            AuditVO auditVO = auditVOList.get(i);
            Long id = auditVO.getAuditUserId();
            auditUserIdList.add(id);
            UserVO userVO = userMapper.selectById(id);
            auditUsernameList.add(userVO.getUsername());
        }
        StringJoiner stringJoiner = new StringJoiner(",");
        for(String username : auditUsernameList){
            stringJoiner.add(username);
        }
        appVO.setAuditUsernameList(stringJoiner.toString());
        appVO.setAuditUserIdList(auditUserIdList);
    }

    @Override
    public void cancel(Long id) {
        log.debug("撤销申请单业务：参数={}",id);
        App app = new App();
        app.setId(id);
        app.setStatus(AppStatusEnum.CANCEL.getCode());
        app.setUpdateTime(new Date());
        appMapper.update(app);
        log.debug("删除审批单业务:参数={}",id);
        auditMapper.delete(id);
    }

    @Override
    public void distribute(Long applicationId, Long vehicleId) {
        log.debug("分配车辆业务：参数={}，{}", applicationId,vehicleId);
        App app = new App();
        app.setId(applicationId);
        app.setVehicleId(vehicleId);
        app.setStatus(AppStatusEnum.ALLOCATION.getCode());
        app.setUpdateTime(new Date());
        appMapper.update(app);
        //同时需要更新车辆状态
        Vehicle vehicle = new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setStatus("2");
        vehicle.setUpdateTime(new Date());
        vehicleMapper.updateVehicle(vehicle);
    }

    @Override
    public void backVehicle(Long id, Long vehicleId) {
        log.debug("还车业务：参数={}，{}", id,vehicleId);
        App app = new App();
        app.setId(id);
        //可以将申请单中的车辆id修改为null(如须改为null 需要重新写一个含test="vehicleId == null"的SQL语句)
        app.setVehicleId(null); // 也可不修改 看业务需求
        app.setStatus(AppStatusEnum.END.getCode());
        app.setUpdateTime(new Date());
        //appMapper.update(app);
        appMapper.backVehicle(app);

        //同时需要更新车辆状态
        Vehicle vehicle = new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setStatus("1");
        vehicle.setUpdateTime(new Date());
        vehicleMapper.updateVehicle(vehicle);
    }


}
